// Created by carolsail

import { ENUM_UI_TYPE } from './../Enum';
import { StaticInstance } from './../StaticInstance';
import BaseLayer from '../layer/Baselayer';
import PoolManager from './PoolManager';
import MainLayer from '../layer/MainLayer';
import SkinLayer from '../layer/SkinLayer';
import { privacyChannel } from '../Script/privacyChannel';
import LoadingLayer from '../layer/LoadingLayer';

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIManager extends cc.Component {

    private uiMap = new Map<ENUM_UI_TYPE, BaseLayer>()

    protected onLoad(): void {
        StaticInstance.setUIManager(this)
    }
    toMian() {
        let delay = (privacyChannel.channel == "ks" || privacyChannel.channel == "ks1") ? 1 : 0
        this.scheduleOnce(() => {
            this.node.getChildByName("LoadingLayer").active = false
            this.node.getChildByName("MenuLayer").active = true
            if (cc.find("Canvas/UI/ruanzhu")) {
                cc.find("Canvas/UI/ruanzhu").removeFromParent()
            }
        }, delay)
        switch (privacyChannel.channel) {
            case "oppo1":
                if (privacyChannel.getChannel().getCtrl("p13")) {
                    this.schedule(() => {
                        privacyChannel.getChannel().showVideo(() => {

                        })
                    }, 10, cc.macro.REPEAT_FOREVER, 0)
                }
                break
        }

    }
    // init() {
    //     for (let type in ENUM_UI_TYPE) {
    //         const node: cc.Node = PoolManager.instance.getNode(ENUM_UI_TYPE[type], this.node)
    //         if (node && !this.uiMap.has(ENUM_UI_TYPE[type])) {
    //             node.active = false
    //             node.addComponent(ENUM_UI_TYPE[type])
    //             this.uiMap.set(ENUM_UI_TYPE[type], node.getComponent(ENUM_UI_TYPE[type]))
    //         }
    //     }
    // }

    init() {

        for (let type in ENUM_UI_TYPE) {
            console.log("type", ENUM_UI_TYPE[type])
            const node: cc.Node = PoolManager.instance.getNode(ENUM_UI_TYPE[type], this.node)
            if (node && !this.uiMap.has(ENUM_UI_TYPE[type])) {
                node.active = false
                node.addComponent(ENUM_UI_TYPE[type])
                this.uiMap.set(ENUM_UI_TYPE[type], node.getComponent(ENUM_UI_TYPE[type]))
            }
        }

        this.node.getChildByName("MenuLayer").active = false
        this.node.getChildByName("LoadingLayer").active = true
        this.node.on("agree", () => {
            this.toMian()
            cc.sys.localStorage.setItem("privacy", 1)
        }, this, true)
        if (cc.sys.localStorage.getItem("privacy") != 1 && privacyChannel.channel != "ks" && privacyChannel.channel != "ks1" && (privacyChannel.channel == "oppo" || privacyChannel.channel == "oppo1" || privacyChannel.channel == "vivo" || privacyChannel.channel == "vivo1" || privacyChannel.channel == "xm")) {
            cc.resources.load('privacy/privacyWin', cc.Prefab, (err, data: cc.Prefab) => {
                if (err) {
                    console.error(err);
                    return;
                }
                console.log("注入成功")
                let node = cc.instantiate(data);
                cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(node);
            });

        } else {
            if (privacyChannel.channel == "ks" || privacyChannel.channel == "ks1") {
                // cc.find("Canvas/UI/MenuLayer/title").getComponent(cc.Label).string = privacyChannel.getChannel().gameName
                // console.error("快手更改游戏名字成功")
            }
            this.toMian()
        }


    }

    toggle(key: ENUM_UI_TYPE, status: boolean = true, callback?: () => void) {
        if (this.uiMap.has(key)) {
            const layer = this.uiMap.get(key)
            status ? layer.show() : layer.hide()
            callback && callback()
        }
    }

    isActive(key: ENUM_UI_TYPE) {
        if (this.uiMap.has(key)) {
            return this.uiMap.get(key).node.active
        }
        return false
    }

    getActiveTypes() {
        const types: ENUM_UI_TYPE[] = []
        this.uiMap.forEach((layer: BaseLayer, type: ENUM_UI_TYPE) => {
            if (this.isActive(type)) types.push(type)
        })
        return types
    }

    setMainCurrenLevel() {
        const layer: MainLayer = this.uiMap.get(ENUM_UI_TYPE.MAIN) as MainLayer
        layer?.setCurrentLevel()
    }

    setSkinReset() {
        const layer: SkinLayer = this.uiMap.get(ENUM_UI_TYPE.SKIN) as SkinLayer
        layer?.reset()
    }
}
